package kg.aibek.projecteuler.p10;

public class Problem37 {
	public void solve() {
		int count = 0;
		long sum = 0;
		int i = 21;
		while (true) {
			if (isPrime(i) && isTruncatable(i)) {
				count++;
				System.out.println(i);
				sum += i;
			}
			if (count == 11) {
				break;
			}
			i += 2;
		}
		System.out.println(sum);
	}

	private boolean isTruncatable(int number) {
		int a = number;
		while (a > 0) {
			a = a / 10;
			if (!isPrime(a))
				return false;
		}

		int b = 10;
		while (b < number) {
			if (!isPrime(number % b)) {
				return false;
			}
			b = b * 10;
		}
		return true;
	}

	private boolean isPrime(int number) {
		if (number == 1)
			return false;
		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0)
				return false;
		}

		return true;
	}

	public static void main(String[] args) {
		Problem37 p = new Problem37();
		p.solve();
	}
}
